package com.product.util;

import java.sql.*;
import java.util.ResourceBundle;

public class DBUtil {
    //静态变量在加载类的时候执行
    //自上而下的顺序
    //属性资源绑定
    private static ResourceBundle bundle=ResourceBundle.getBundle("resources.jdbc");
    //根据key获取values
    private static String driver=bundle.getString("driver");
    private static String url=bundle.getString("url");
    private static String user=bundle.getString("user");
    private static String password=bundle.getString("password");

static {

    try {
        //注册驱动
        Class.forName(driver);
            } catch (ClassNotFoundException e ) {
        throw new RuntimeException(e);
            }

}
   /*
    获取数据库连接对象
    返回return coon
    throws SQLException
   *///Connection conn=  DriverManager.getConnection(url,user,password);
        public static Connection getConnection() throws SQLException {
         Connection coon = DriverManager.getConnection(url,user,password);
            return coon;

    }

    //释放资源
    /*
Connection conn连接对象
 Statement ps 数据库操作对象
 ResultSet rs) 结果集对象
     */
    public static void close(Connection conn, PreparedStatement ps , ResultSet rs){
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ps!=null){
                try {
                ps.close();
            } catch (SQLException e) {
                    throw new RuntimeException(e);
            }
        }

            if (conn!=null){
                try {
               conn.close();
            }
            catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }


    }
}
